import api from '../../api/index'
import common from "../../common/index";


// index.ts
// 获取应用实例
// const app = getApp<IAppOption>()
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
const app = getApp()
Component({
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    navBarHeight: app.globalData.navBarHeight,

    historyPageShow: false,
    chatPageShow: false,
    pcenterPageShow: false,

    uid: '',
    chatting: false,
  },

  pageLifetimes: {
    async show() {
      const user = common.getAppUserInfo()
      this.data.uid = user.uid
      this.setData({
        uid: user.uid
      })
      if (this.data.uid) {
        await app.globalData.checkLoginPromise
        const data = await this.getUserPortrait(this.data.uid)
        if (!data) {
          wx.reLaunch({
            url: '/pages/my-archive/my-archive?single=true',
          })
        }
      }
    }
  },
  methods: {
    onLoad(){
      console.log('onLoad')
      wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      })
    },
    async getUserPortrait(uid: string) {
      let result = null
      try {
        const res = await api.getPortrait({uid})
        if (res.data) {
          result = res.data
        }
      } catch (e) {
        console.log(e)
      }
      return result
    },
    /**
     * 显示历史会话浮层页面
     */
    showChatHistoryPage(){
      if (!this.data.uid) {
        wx.showToast({
          title: '请先登录',
          icon: 'none'
        })
        return
      }
      this.setData({
        historyPageShow: true
      })
    },
    /**
     * 隐藏历史会话浮层页面
     */
    closeHistoryPage(){
      const comp = this.selectComponent('.chat-history')
      comp.clearHistoryList()
      this.setData({
        historyPageShow: false
      })
    },
    /**
     * 显示会话浮层页面
     */
    showChatPage(){
      this.setData({
        chatPageShow: true
      })
    },
    /**
     * 隐藏会话浮层页面
     */
    closeChatPage(){
      this.setData({
        chatPageShow: false
      })
    },
    showPcenterPage() {
      this.setData({
        pcenterPageShow: true
      })
    },
    closePcenterPage() {
      this.setData({
        pcenterPageShow: false
      })
    },
    handleEnterChatPage(e: BaseEvent){
      const {condition, text} = e.detail
      this.setData({
        chatPageShow: true
      }, () => {
        const comp = this.selectComponent('.chat-comp')
        comp.autoSendNewQuestion(condition, text)
      })
    },
    afterEnterHistory(){
      const comp = this.selectComponent('.chat-history')
      comp.getHistoryList()
    },
    handleChatStart() {
      this.setData({
        chatting: true
      })
    },
    handleChatStop() {
      this.setData({
        chatting: false
      })
    },
    /**
     * 历史会话浮层点击某个会话
     * @param e BaseEvent
     */
    enterTopic(e: BaseEvent){
      const topic = e.detail
      this.setData({
        chatPageShow: true
      }, () => {
        const chatComp = this.selectComponent('.chat-comp')
        chatComp.enterTopic(topic)
      })
      this.closeHistoryPage()
    },
    enterNewTopic() {
      if (!this.data.uid) {
        wx.showToast({
          title: '请先登录',
          icon: 'none'
        })
        return
      }
      const chatComp = this.selectComponent('.chat-comp')
      chatComp.enterNewTopic()
    },
    /**
     * 跳转到灵感发现页面
     */
    handleGotoInspectionPage() {
      wx.switchTab({
        url: '/pages/inspection/inspection'
      })
    },
    /**
     * 跳转到灵感详情页面
     * @param e
     */
    handleGotoInspectionDetailPage(e: BaseEvent){
      const clickedItem: any = e.detail
      const params = {
        uid: clickedItem.uid,
        id: clickedItem.id,
        author_id: clickedItem.username
      };
      wx.navigateTo({
        url: `/pages/inspection/detail/detail?id=${params.id}&uid=${params.uid}&author_id=${params.author_id}`
      });
    },
  },
})
